package com.lovo.controller;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.lovo.model.Staff;
import com.lovo.model.Store;
import com.lovo.service.StaffService;
import com.lovo.service.StoreService;
import com.lovo.service.impl.StaffServiceImpl;
import com.lovo.service.impl.StoreServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

@WebServlet(name = "StaffServlet",urlPatterns = "/staff.do")
public class StaffServlet extends HttpServlet {
    private StaffService ss = new StaffServiceImpl();
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request,response);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setContentType("text/html;charset=utf-8");

        String method = request.getParameter("method");
        if (method == null || "".equals(method)) {
            showAll(request, response);
        }
    }
    private void showAll(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        int store_id = Integer.parseInt(request.getParameter("store_id"));
        Staff staff = new Staff();
        store_id = 1;
        List<Staff> list = ss.getStaffById(store_id);
        ObjectMapper objectMapper = new ObjectMapper();
        String json = objectMapper.writeValueAsString(list);
        response.getWriter().write(json);
    }
}